<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="作者" prop="author">
      <el-input v-model="dataForm.author" placeholder="作者"></el-input>
    </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
      </el-form-item>
    <el-form-item label="包路径" prop="basePackage">
      <el-input v-model="dataForm.basePackage" placeholder="包路径"></el-input>
    </el-form-item>
      <el-form-item label="表前缀" prop="tablePrefix">
        <el-input v-model="dataForm.tablePrefix" placeholder="多个用逗号分隔,例如: tb_,sys_,c_等"></el-input>
      </el-form-item>
    <el-form-item label="groupId" prop="groupId">
      <el-input v-model="dataForm.groupId" placeholder="pom文件groupId"></el-input>
    </el-form-item>
    <el-form-item label="artifactId" prop="artifactId">
      <el-input v-model="dataForm.artifactId" placeholder="pom文件artifactId"></el-input>
    </el-form-item>
      <el-form-item label="持久层接口后缀" prop="daoSuffix">
        <el-input v-model="dataForm.daoSuffix" placeholder="持久层接口后缀"></el-input>
      </el-form-item>
      <el-form-item label="实体类后缀" prop="pojoSuffix">
        <el-input v-model="dataForm.pojoSuffix" placeholder="实体类后缀"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          author: '',
          email: '',
          basePackage: '',
          tablePrefix: '',
          daoSuffix: '',
          pojoSuffix: '',
          groupId: '',
          artifactId: '',
          isEnable: '',
          createUserId: '',
          createTime: ''
        },
        dataRule: {
          author: [
            { required: true, message: '作者不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          basePackage: [
            { required: true, message: '包路径不能为空', trigger: 'blur' }
          ],
          tablePrefix: [
            { required: true, message: '表前缀不能为空', trigger: 'blur' }
          ],
          groupId: [
            { required: true, message: 'pom文件groupId不能为空', trigger: 'blur' }
          ],
          artifactId: [
            { required: true, message: 'pom文件artifactId不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/config/preference/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.author = data.data.author
                this.dataForm.email = data.data.email
                this.dataForm.basePackage = data.data.basePackage
                this.dataForm.tablePrefix = data.data.tablePrefix
                this.dataForm.daoSuffix = data.data.daoSuffix
                this.dataForm.pojoSuffix = data.data.pojoSuffix
                this.dataForm.groupId = data.data.groupId
                this.dataForm.artifactId = data.data.artifactId
                this.dataForm.isEnable = data.data.isEnable
                this.dataForm.createUserId = data.data.createUserId
                this.dataForm.createTime = data.data.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/config/preference/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'author': this.dataForm.author,
                'email': this.dataForm.email,
                'basePackage': this.dataForm.basePackage,
                'tablePrefix': this.dataForm.tablePrefix,
                'daoSuffix': this.dataForm.daoSuffix,
                'pojoSuffix': this.dataForm.pojoSuffix,
                'groupId': this.dataForm.groupId,
                'artifactId': this.dataForm.artifactId,
                'isEnable': this.dataForm.isEnable,
                'createUserId': this.dataForm.createUserId,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
